﻿using OpenTK;
using OpenTK.Graphics.OpenGL;

namespace Chimp.Core
{
    public class ChimpPostFX : ChimpBase
    {
        public int LayerID = -1;

        public ChimpPostFX()
        {
            EffectTex = new ChimpTexture2D((int) ChimpApp.W, (int) ChimpApp.H);
            EffectRT = new ChimpRenderTarget();
        }

        public ChimpEffect Effect { get; set; }
        public ChimpTexture2D EffectTex { get; set; }
        public ChimpRenderTarget EffectRT { get; set; }
        public ChimpTexture2D TempFB { get; set; }
        public ChimpTexture2D Src { get; set; }
        public virtual void OnPreRender()
        {
            
        }
        public ChimpTexture2D Render(ChimpTexture2D src)
        {
            Src = src;
            SetPars();
            OnPreDrawPostFX(src);
          
            if (LayerID > -1)
            {
                if (TempFB == null)
                {
                    TempFB = new ChimpTexture2D((int) ChimpApp.W, (int) ChimpApp.H);
                }

                EffectRT.Bind();
                EffectRT.AttachTexture(TempFB);
                GL.Clear(ClearBufferMask.ColorBufferBit);
                foreach (ChimpBase o in Scene.ObjectList)
                {
                    if (o.LayerID == LayerID)
                    {
                        o.Draw(-Scene.ViewPos);
                    }
                }

                EffectRT.Unbind();
            }
            EffectRT.Bind();
            EffectRT.AttachTexture(EffectTex);
            if (LayerID == -1)
            {

                OnDrawPostFX(src);
                OnRender();
            }
            else
            {
                ChimpDraw.BlendMode = ChimpBlend.Alpha;
                ChimpDraw.Image(new Vector4(0, 0, ChimpApp.W, ChimpApp.H), src, new Vector4(1, 1, 1, 1));
                ChimpDraw.BlendMode = ChimpBlend.Lighten;
                OnDrawPostFX(TempFB);
                OnRender();
            }
            EffectRT.Unbind();
            return EffectTex;
        }
        public virtual void SetPars()
        {
            Effect.Use();
            OnSetPars();
            Effect.Fin();
        }
        public virtual void OnSetPars()
        {
            
        }
        public virtual void OnPreDrawPostFX(ChimpTexture2D tex)
        {
        }
        public virtual void OnRender()
        {
            
        }
        public void UseShader()
        {
            Effect.Use();
        }
        public void StopShader()
        {
            Effect.Fin();
        }
        public void Fill(ChimpBlend b,params ChimpTexture2D[] tex)
        {
            int i = 0;
            foreach(var t in tex)
            {
                if ( i > 0 )
                {
                    t.Bind(i);
                }
                i++;
            }
            ChimpDraw.BlendMode = b;
            float a = 0.0f;
            float s = 1.0f;
            Vector4 col = new Vector4(1,1,1,1);
            Vector4 pos = new Vector4(0, 0, ChimpApp.W, ChimpApp.H);
            ChimpDraw.BasicImage(ref pos, ref a,ref s, ref col,
                                 tex[0],false);
            i = 0;
            foreach(var t in tex)
            {
                if(i>0)
                {
                  t.Unbind(i);
                }
                i++;
            }
        }
        public virtual void OnDrawPostFX(ChimpTexture2D src)
        {
        }
    }
}